package com.moyq5.spring.boot.app.register;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 默认任务组信息存储器
 * @author Moyq5
 * @date 2019年2月25日
 */
public class AppGroupStoreDefault implements AppGroupStore {
	private static final Logger log = LoggerFactory.getLogger(AppGroupStoreDefault.class);
	
	private final ConcurrentHashMap<Serializable, AppGroup> groups = new ConcurrentHashMap<>();
	
	@Override
	public AppGroup get(Serializable groupId) {
		AppGroup dst = null;
		if (null != (dst = groups.get(groupId))) {
			return dst;
		}
		return null;
	}

	@Override
	public void add(AppGroup group) {
		synchronized(groups) {
			if (null != groups.get(group.groupId())) {
				throw new RuntimeException("应用组已经存在：" + group.groupId());
			}
			log.debug("添加应用组：{}", group.groupId());
			groups.put(group.groupId(), group);
		}
	}

	@Override
	public List<AppGroup> list() {
		return new ArrayList<AppGroup>(groups.values());
	}

}
